<?php
namespace Mobile\Controller;
use Think\App;
use Think\Controller;

class OrderController extends Controller
{

	public function getOrder(){
		$userId = getUserId();
		$list = D('Order')->getList($userId);
        $list ?  $status = 1 :  $status = 0;
		echo json_encode(array('list' => $list, 'error' => $status));
	}

  	public function createOrder(){
        $sessionId= I('sessionId');
        $userName= I('userName');
        $telNumber= I('telNumber');
        $address= I('address');
        $goods = I('goods');
        $shopId = I('shopId');
        $fid = I('fid');
        $couponId = I('couponId');
        $latitude = I('latitude');
        $longitude = I('longitude');
        $get_time = I('get_time');
        $xcx =  new \Mobile\Model\xcx();
        //计算金额
        $goodsInfo = $xcx->countMoney($goods,$couponId); 
        $total_fee = $goodsInfo['money'] ;
        //从缓存里获取openid
        $session = S($sessionId);
        $openid = $session['openid'];
        //订单号
        $number = $xcx->createNoncestr(10).date('YmdHi'); 
        //缓存prepay_id ，用于回调时发送模板消息
        S('prepay_id'.$number,$fid);
        //添加订单
        $time = time();
        $add  = array(
            'order_number' => $number,
            'user_id' => $session['user_id'],
            'pay_time' => $time,
            'pay_status' => 1,
            'pay_money' => $total_fee,
            'time' => $time,
            'order_status' => 1,
             );
        $add1  = array(
            'order_number' => $number,
            'user_name' => $userName,
            'mobile' => $telNumber,
            'shop_id' => $shopId,
            'address' => $address,
            'cart' =>  $goodsInfo['cart'],
            'user_id' => $session['user_id'],
            'price' => $total_fee,
            'time' => $time,
            'coupon_id' => $couponId,
            'latitude' => $latitude,
            'longitude' => $longitude,
            'get_time' => $get_time,
             );
        $id = M('order')->add($add);
        M('order_detail')->add($add1);
        if($id){
            $res = array('error' => 1, 'id' => $id);
        }else{
            $res = array('error' => 2, 'id' => $id);
        }
        echo json_encode($res); 
    }

}

